package rexsee.dialog;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import rexsee.core.browser.Browser;
import rexsee.core.browser.clazz.JavascriptInterface;
import rexsee.core.menu.PopMenu;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;

public class RexseeButtonDialog implements JavascriptInterface {

	public static final String INTERFACE_NAME = "ButtonDialog";
	@Override
	public String getInterfaceName() {
		return mBrowser.application.resources.prefix + INTERFACE_NAME;
	}
	@Override
	public JavascriptInterface getInheritInterface(Browser childBrowser) {
		return this;
	}
	@Override
	public JavascriptInterface getNewInterface(Browser childBrowser) {
		return this;
	}

	public static final String EVENT_ONBUTTONDIALOGDISMISSED = "onButtonDialogDismissed";

	private final Browser mBrowser;
	private final Context mContext;
	private final HashMap<String, ButtonDialog> mMap;
	private boolean syncTag = false;

	public RexseeButtonDialog(Browser browser) {
		super();
		mBrowser = browser;
		mContext = browser.getContext();
		mMap = new HashMap<String, ButtonDialog>();
		mBrowser.eventList.add(EVENT_ONBUTTONDIALOGDISMISSED);
	}

	//JavaScript Interface

	public void show(String menuIds, final String style) {
		show(null, menuIds, style);
	}
	public void show(String id, String menuIds, final String style) {
		final String dialogId = (id != null) ? id : menuIds;
		String[] ids = menuIds.split(",");
		if (ids.length == 0) return;
		final PopMenu[] menus = new PopMenu[ids.length];
		for (int i = 0; i < ids.length; i++) {
			menus[i] = mBrowser.menu.menus.get(ids[i]);
		}
		if (mMap.containsKey(dialogId)) return;
		syncTag = false;
		((Activity) mContext).runOnUiThread(new Runnable() {
			@Override
			public void run() {
				try {
					ButtonDialog dialog = new ButtonDialog(mBrowser, dialogId, style, menus);
					dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
						@Override
						public void onDismiss(DialogInterface dialog) {
							mMap.remove(dialogId);
							mBrowser.eventList.run(EVENT_ONBUTTONDIALOGDISMISSED, new String[]{dialogId});
						}
					});
					dialog.start();
					mMap.put(dialogId, dialog);
				} catch (Exception e) {
					mBrowser.exception(getInterfaceName(), e);
				}
				syncTag = true;
			}
		});
		while (!syncTag) {
			try {
				Thread.sleep(100);
			} catch (Exception e) {

			}
		}
		return;
	}
	public void dismiss(String id) {
		if (!mMap.containsKey(id)) return;
		ButtonDialog dialog = mMap.get(id);
		if (dialog != null) dialog.dismiss();
		mMap.remove(id);
	}
	public void dismissAll() {
		Set<String> keys = mMap.keySet();
		Iterator<String> iterator = keys.iterator();
		while (iterator.hasNext()) {
			String key = iterator.next();
			ButtonDialog dialog = mMap.get(key);
			if (dialog != null) dialog.dismiss();
		}
		mMap.clear();
	}
	public void setStyle(String id, String style) {
		if (!mMap.containsKey(id)) return;
		mMap.get(id).setStyle(style);
	}
	public String getStyle(String id, String name) {
		return (!mMap.containsKey(id)) ? "" : mMap.get(id).getStyle(name);
	}
	public boolean exists(String id) {
		return mMap.containsKey(id);
	}
	public int size() {
		return mMap.size();
	}
	public String getIds() {
		String rtn = "";
		Set<String> ids = mMap.keySet();
		Iterator<String> iterator = ids.iterator();
		while (iterator.hasNext()) {
			if (!rtn.equals("")) rtn += ",";
			rtn += "\"" + iterator.next() + "\"";
		}
		return "[" + rtn + "]";
	}

}
